package vip.xiaozhao.paotui.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaozhao.paotui.intf.dto.PaoTuiDetailDO;
import vip.xiaozhao.paotui.intf.dto.PaoTuiSelectionDO;
import vip.xiaozhao.paotui.intf.entity.PaotuiDetail;
import vip.xiaozhao.paotui.intf.enums.*;
import vip.xiaozhao.paotui.intf.mapper.PaoTuiDetailMapper;
import vip.xiaozhao.paotui.intf.service.PaotuiDetailService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class PaotuiDetailServiceImpl implements PaotuiDetailService {
    @Resource
    private PaoTuiDetailMapper paoTuiDetailMapper;


    @Override
    public int insert(PaotuiDetail detail) {
        return paoTuiDetailMapper.insert(detail);
    }

    @Override
    public PaotuiDetail loadById(int id) {
        return paoTuiDetailMapper.loadById(id);
    }

    @Override
    public PaotuiDetail loadVersionLockById(int id) {
        return paoTuiDetailMapper.loadVersionLockById(id);
    }

    @Override
    public int updateByVersionLock(PaotuiDetail paotuiDetail) {
        return paoTuiDetailMapper.updateByVersionLock(paotuiDetail.getId());
    }

    @Override
    public int update(PaotuiDetail paotuiDetail) {
        return 0;
    }

    @Override
    public PaoTuiDetailDO getList(PaoTuiSelectionDO paoTuiSelectionDO, int pageId, int size) {
        //任务到期时间的判断 (注意，这里面有一个动态参数0 ，可以设置成一个较长的固定时间，也可以不使用枚举，直接使用sql语句)
        Date expectTime  = PaotuiTimeTypeEnum.getDateByType(paoTuiSelectionDO.getTime());
        int offset = (pageId - 1) *size;
        List<PaotuiDetail> detailList = paoTuiDetailMapper.loadListByMultipleConditions(paoTuiSelectionDO,expectTime ,offset, size +1);
        PaoTuiDetailDO paoTuiDetailDO = new PaoTuiDetailDO();

        paoTuiDetailDO.setHasMore(false);
        if (detailList.size() == size+1) {
            paoTuiDetailDO.setHasMore(true);
            detailList.remove(size);
        }
        paoTuiDetailDO.setPaotuiDetailList(detailList);
        return paoTuiDetailDO;
    }

    @Override
    public PaoTuiDetailDO getNextList(PaoTuiSelectionDO paoTuiSelectionDO, int lastId, int size) {
        //根据lastId对应的记录查下一页 避免数据的重复展示
        PaotuiDetail lastPaotuiDetail = paoTuiDetailMapper.loadById(lastId);
        if (lastPaotuiDetail == null || lastPaotuiDetail.getStatus() == 0) {
            return null;
        }


        //1.查出排序项相同的情况（金额，最新） 并筛选lastId之后的记录
        List<PaotuiDetail> sameList = paoTuiDetailMapper.loadListBySameConditions(paoTuiSelectionDO,lastId,lastPaotuiDetail.getAmount().add(lastPaotuiDetail.getFee()),
                lastPaotuiDetail.getExpectTime());
        //2.如果记录大于等于10条
        //等于10条要插是否还有后续小于的
        if (sameList != null && sameList.size() >= size) {
            PaoTuiDetailDO paoTuiDetailDO = new PaoTuiDetailDO();
            if (sameList.size() == size) {
                List<PaotuiDetail> afterList = paoTuiDetailMapper.loadAfterListByMultipleConditions(paoTuiSelectionDO,lastPaotuiDetail.getAmount().add(lastPaotuiDetail.getFee()),
                        lastPaotuiDetail.getExpectTime(),1);
                paoTuiDetailDO.setHasMore(false);
                if (afterList != null && afterList.size() == 1) {
                    paoTuiDetailDO.setHasMore(true);
                }
                paoTuiDetailDO.setPaotuiDetailList(sameList);

            } else {
                while (sameList.size() > size) {
                    sameList.remove(sameList.size() - 1);
                }
                paoTuiDetailDO.setHasMore(true);
                paoTuiDetailDO.setPaotuiDetailList(sameList);
            }
            return paoTuiDetailDO;
        }
        int smallListSize = sameList== null? 0:sameList.size();

        //3.如果记录小于10条，筛选排序优先级较低 即 < 最后一条记录对应的排序项 的记录
        //补全10条（查11条，有11条则 HasMore字段为true）
        List<PaotuiDetail> afterList = paoTuiDetailMapper.loadAfterListByMultipleConditions(paoTuiSelectionDO,lastPaotuiDetail.getAmount().add(lastPaotuiDetail.getFee()),
                lastPaotuiDetail.getExpectTime(),size - smallListSize + 1);
        List<PaotuiDetail> resList = new ArrayList<>();
        if (sameList != null) {
            resList.addAll(sameList);
        }
        if (afterList != null) {
            resList.addAll(afterList);
        }

        PaoTuiDetailDO paoTuiDetailDO = new PaoTuiDetailDO();
        paoTuiDetailDO.setPaotuiDetailList(resList);
        if (resList.size() > size) {
            resList.remove(resList.size() - 1);
            paoTuiDetailDO.setHasMore(true);
        } else {
            paoTuiDetailDO.setHasMore(false);
        }

        return paoTuiDetailDO;
    }


}
